package com.zhy.generator.codegen.mybatis.javacontroller.elements;
import com.zhy.generator.codegen.dom.java.FullyQualifiedJavaType;
import com.zhy.generator.codegen.dom.java.JavaVisibility;
import com.zhy.generator.codegen.dom.java.Method;
import com.zhy.generator.codegen.dom.java.TopLevelClass;
import com.zhy.generator.codegen.mybatis.javacontroller.JavaControllerMethodConstant;
import com.zhy.generator.codegen.mybatis.javahelper.JavaHelperMethodConstant;

/**
 * @author 源
 */
public class AddMethodGenerator extends AbstractJavaControllerMethodGenerator {
    @Override
    public void addControllerElements(TopLevelClass tlc) {
        Method listMethod = new Method(getMethodStr());
        listMethod.setVisibility(JavaVisibility.PUBLIC);
        tlc.addMethod(listMethod);

        FullyQualifiedJavaType returnType = getReturnType();
        listMethod.setReturnType(returnType);
        tlc.addImportedType(returnType);

        addParameter(tlc, listMethod);

        addMethodAnnotations(tlc, listMethod);

        FullyQualifiedJavaType javaPOType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
        tlc.addImportedType(javaPOType);
        FullyQualifiedJavaType helperType = new FullyQualifiedJavaType(introspectedTable.getHelperType());
        tlc.addImportedType(helperType);

        listMethod.addBodyLine(javaPOType.getShortName() + " po = "+helperType.getShortName()+"."+ JavaHelperMethodConstant.ADDPARAM_TO_PO +"(param);");

        listMethod.addBodyLine("Long n = "+getServiceShortName()+".addBySelective(po);");

        listMethod.addBodyLine("if (n > 0) {");
        listMethod.addBodyLine("   return ResultUtil.jsonMsg(ResultErrCodeEnum.SUCCESS.getStatus(), \"操作成功\", n);");
        listMethod.addBodyLine("}");
        listMethod.addBodyLine("return ResultUtil.jsonMsg(ResultErrCodeEnum.FAIL.getStatus(), \"操作失败\", null);");

    }

    @Override
    public String getMethodStr() {
        return JavaControllerMethodConstant.ADD_METHOD;
    }
}
